home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 009a / snpd0493.zip / COMMCONV.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  3KB  |  98 lines

  1. /*
  2.  * COMMCONV.C
  3.  * Change C++ -comments to C-comments
  4.  *
  5.  * Public domain by Jari Laaksonen (2:221/105.11), 22 Dec 1992
  6.  */
  7.  
  8. #include <stdio.h>
  9.  
  10. int main (int argc, char **argv)
  11. {
  12.   int  Char, cpp_comment = 0, c_comment = 0, in_string = 0;
  13.   char CannotOpen[] = "\nCannot open %s\n\n";
  14.   FILE *InFile, *OutFile = stdout;
  15.  
  16.   if (argc < 2)
  17.   {
  18.     fprintf (stderr, "USAGE: COMMCONV InFile [OutFile]\n");
  19.     return (1);
  20.   }
  21.   if ((InFile = fopen (argv[1], "r")) == NULL)
  22.   {
  23.     fprintf (stderr, CannotOpen, argv[1]);
  24.     return (3);
  25.   }
  26.  
  27.   if (argc == 3)
  28.   {
  29.     if ((OutFile = fopen (argv[2], "w")) == NULL)
  30.     {
  31.       fprintf (stderr, CannotOpen, argv[2]);
  32.       OutFile = stdout;  /* if can't open, output goes to stdout instead */
  33.     }
  34.   }
  35.  
  36.   while ((Char = fgetc (InFile)) != EOF)
  37.   {
  38.     fputc (Char, OutFile);
  39.  
  40.     if (Char == '\"')
  41.       in_string = ! in_string;    /* toggle flag */
  42.  
  43.     if (in_string)                /* we are in a string now */
  44.       continue;
  45.  
  46.     if (Char == '/')              /* slash */
  47.     {
  48.       Char = fgetc (InFile);      /* check next char */
  49.       if (Char == '/')            /* is it start of C++ comment */
  50.       {
  51.         Char = '*';               /* change it to C comment */
  52.         cpp_comment = 1;
  53.       }
  54.       else if (Char == '*')       /* is it start of C comment */
  55.         c_comment = 1;
  56.  
  57.       fputc (Char, OutFile);
  58.  
  59.       if (c_comment || cpp_comment) /* inside C or C++ comment */
  60.       {
  61.         while ((Char = fgetc (InFile)) != '\n') /* rest of the line */
  62.         {
  63.           if (Char == '*' && c_comment)
  64.           {
  65.             int Ch = fgetc (InFile);         /* check next char */
  66.             if (Ch == '/')                   /* is it end of C comment */
  67.               c_comment = 0;
  68.             ungetc (Ch, InFile);             /* put it back to stream */
  69.           }
  70.           fputc (Char, OutFile);
  71.         }
  72.         if (cpp_comment)
  73.         {
  74.           fputs (" *", OutFile);             /* put ending C comment mark */
  75.           if (c_comment)
  76.             fputc (' ', OutFile);
  77.           fputc ('/', OutFile);
  78.           cpp_comment = 0;
  79.         }
  80.         fputc ('\n', OutFile);
  81.       }
  82.     }
  83.     else if (Char == '*' && c_comment)
  84.     {
  85.       Char = fgetc (InFile);
  86.       if (Char == '/')                   /* is it end of C comment */
  87.         c_comment = 0;
  88.       fputc (Char, OutFile);
  89.     }
  90.   } /* while end */
  91.  
  92.   if (argc == 3)
  93.     fclose (OutFile);
  94.   fclose (InFile);
  95.  
  96.   return 0;
  97. }
  98.